package org.study.bookmanagerclientbyswing.api;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.study.bookmanagerclientbyswing.io.request.Borrow.AddBorrowReq;
import org.study.bookmanagerclientbyswing.io.request.PageReq;
import org.study.bookmanagerclientbyswing.io.response.RestBean;
import reactor.core.publisher.Mono;

@Component
public class BorrowApi {

    @Resource
    private WebClient webClient;

    private String baseUrl = "v1/borrow";

    public RestBean addBorrow(AddBorrowReq req) {
        return webClient.post()
                .uri(baseUrl+"/")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean returnBorrow(int id) {
        return webClient.put()
                .uri(baseUrl+"/{id}/return", id)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean getBorrows(PageReq pageReq) {
        return webClient.post()
                .uri(baseUrl+"/list")
                .bodyValue(pageReq)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean getBorrow(int id) {
        return webClient.get()
                .uri(baseUrl+"/{id}", id)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean updateBorrow(int id) {
        return webClient.put()
                .uri(baseUrl+"/{id}", id)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }
}